package com.richard.library.permission;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import java.util.List;

import permissions.dispatcher.PermissionUtils;

/**
 * <pre>
 * Description : 权限请求辅助类
 * Author : admin-richard
 * Date : 2022/10/10 13:43
 * Changelog:
 * Version            Date            Author              Detail
 * ----------------------------------------------------------------------
 * 1.0         2022/10/10 13:43      admin-richard         new file.
 * </pre>
 */
public class PermissionHelper {

    public static Builder init(FragmentManager manager) {
        return new Builder(null, manager);
    }

    public static Builder init(Fragment fragment) {
        return new Builder(fragment.getContext(), fragment.getChildFragmentManager());
    }

    public static Builder init(AppCompatActivity activity) {
        return new Builder(activity, activity.getSupportFragmentManager());
    }

    private void request(FragmentManager manager, String[] permissions, PEvent pEvent,
                         ShowRationale showRationale, OnDenied onDenied, OnNeverAskAgain onNeverAskAgain) {
        PermissionHandler.request(manager, permissions, pEvent, showRationale, onDenied, onNeverAskAgain);
    }

    public static class Builder {
        private Context context;
        private final FragmentManager manager;
        private String[] permissions;
        private ShowRationale showRationale;
        private OnDenied onDenied;
        private OnNeverAskAgain onNeverAskAgain;

        public Builder(Context context, FragmentManager manager) {
            this.context = context;
            this.manager = manager;
        }

        public Builder setPermission(String... permission) {
            this.permissions = permission;
            return this;
        }

        public Builder setShowRationale(ShowRationale showRationale) {
            this.showRationale = showRationale;
            return this;
        }

        public Builder setOnDenied(OnDenied onDenied) {
            this.onDenied = onDenied;
            return this;
        }

        public Builder setOnNeverAskAgain(OnNeverAskAgain onNeverAskAgain) {
            this.onNeverAskAgain = onNeverAskAgain;
            return this;
        }

        public void request(){
            this.request(null);
        }

        public void request(PEvent pEvent) {
            if (context == null) {
                List<Fragment> fragments = manager.getFragments();
                if (!fragments.isEmpty()) {
                    context = fragments.get(0).getContext();
                }
            }

            if (context != null) {
                if (PermissionUtils.hasSelfPermissions(context, permissions)) {
                    //执行对应事件
                    if(pEvent != null){
                        pEvent.run();
                    }
                    return;
                }
            }

            new PermissionHelper().request(manager, permissions, pEvent, showRationale, onDenied, onNeverAskAgain);
        }
    }

}
